﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using CoreEA.LoginInfo;
using LinqHelper;

namespace TestReportView
{
    public partial class Form1 : Form
    {
        IDbConnection conn = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            #region Prepare the db connection
            CoreEA.ICoreEAHander core = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.SqlCE35).X_Handler;
            core.Open(new LoginInfo_SSCE()
            {
                DbName = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "TestWithImage.sdf",
            });
            if (!core.IsOpened)
            {
                MessageBox.Show("Can't open db file");
                return;
            }
            conn = core.GetConnection();

            #endregion

            //reset the report viewer

            this.reportViewer1.Reset();


            //set processing to local mode

            this.reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            TestWithImage t = new TestWithImage(conn);

            #region Insert a record

            //TestTable1 testData = new TestTable1();
            //testData.Name = "TestName";
            //testData.ID = 1;
            //testData.Image = XLCS.Common.ImageProcess.ConvertImageToByteArray(
            //    new Bitmap(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "test.png"),
            //    System.Drawing.Imaging.ImageFormat.Jpeg);

            //t.TestTable1.InsertOnSubmit(testData);


            //TestTable1 testData_2 = new TestTable1();
            //testData_2.Name = "TestName2";
            //testData_2.ID = 2;
            //testData_2.Image = XLCS.Common.ImageProcess.ConvertImageToByteArray(
            //    new Bitmap(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "test.png"),
            //    System.Drawing.Imaging.ImageFormat.Jpeg);

            //t.TestTable1.InsertOnSubmit(testData_2);


            //t.SubmitChanges();
            #endregion

            //Select data via linq to sql
            var a = from b in t.TestTable1
                    select b;
            if (a.Count() < 1)
            {
                MessageBox.Show("No data ");
                return;
            }

            //convert linq to sql query result to DataTable(Ado.net type)
            DataTable dt = a.ToDataTable();
            dt.TableName = "MyTestTable";

            ReportDataSource source = new ReportDataSource("MyTestTable", dt);

            reportViewer1.LocalReport.DataSources.Add(source);
            this.reportViewer1.RefreshReport();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            TestWithImage t = new TestWithImage(conn);
            var a = from b in t.TestTable1
                    where b.ID>1
                    select b;

            if (a.Count() < 1)
            {
                MessageBox.Show("No data ");
                return;
            }

            //convert linq to sql query result to DataTable(Ado.net type)
            DataTable dt = a.ToDataTable();
            dt.TableName = "MyTestTable";

            ReportDataSource source = new ReportDataSource("MyTestTable", dt);

            reportViewer1.LocalReport.DataSources.Add(source);
            this.reportViewer1.RefreshReport();
        }
    }
}
